const http = require('./http_client');

class ZhuoJianYun {
    constructor(cfg) {
        this.cfg = cfg;
    }
}

class ZhuoJianYunSms extends ZhuoJianYun {
    constructor(cfg) {
        super();
        this.cfg = cfg;
    }
    //发送短信
    async sendSms(mobile, params) {
        try {
            let data = {
                accessKey: this.cfg.accessKey,
                accessSecret: this.cfg.accessSecret,
                signCode: this.cfg.signCode,
                templateCode: this.cfg.templateCode,
                mobile: mobile,
                params: params,
                msgType: this.cfg.msgType
            };
            let url = 'https://api.juncyun.com/api/msgService/sendSms';
            if (Array.isArray(mobile)) {
                url = 'https://api.juncyun.com/api/msgService/sendSmsBatch';
            }
            let ret = await http.httpsPost(url, JSON.stringify(data), 'json');
            let retStr = ret.body.toString();
            console.log(retStr);
            let retObj = JSON.parse(retStr);
            return [retObj.code==200?0:1, retObj.msg];
        } catch (error) {
            if (global.log) {
                global.log.error(error);
            } else {
                console.error(error);
            }
            return [1, '调用短信发送失败'];
        }
    }
}
// let cfg = {
//     "accessKey": "NDYw*******",
//     "accessSecret": "MDQyOTQ1OTM3*********************",
//     "signCode": "b1d8acef",
//     "templateCode": "65c1b941",
//     "msgType": 1
// }

module.exports.ZhuoJianYunSms = ZhuoJianYunSms;
